/**
* Created by jinxs on 14-7-8.
* 依赖
* ui.core.js
* ui.mask.js
* ui.position.js
*/
(function ($) {
    /**
    * @class Loading组件
    * @constructor uiLoading
    * @type {{alert: Function, confirm: Function, prompt: Function}}
    */
    $.uiLoading = {
        /**
        * @summary 添加loading
        * @name uiLoading#add
        * @function
        * @param options {Object} 组件的配置对象
        * @returns {jQuery} 组件对象
        */
        add: function (options) {
            options = options || {};
            options = $.extend({}, $.uiLoading.defaults, options);
            return _add(options);
        },
        /**
        * @summary 显示loading
        * @name uiLoading#show
        * @function
        * @param options {Object} 组件的配置对象
        * @returns {jQuery} 组件对象
        */
        show: function (options) {
            options = options || {};
            options = $.extend({}, $.uiLoading.defaults, options);
            var loading = _show(options);
            return loading;
        },
        /**
        * @summary 隐藏loading
        * @name uiLoading#hide
        * @function
        * @param options {Object} 组件的配置对象
        * @returns {jQuery} 组件对象
        */
        hide: function (options) {
            options = options || {};
            options = $.extend({}, $.uiLoading.defaults, options);
            var container = options.container;
            var contID=container.getElemID();
            var loading = $.getByParentID(contID,options.loadingID);
            if (loading.length > 0) {
                loading.hide();
                if (container[0].uiposition != undefined) {
                    container.css('position', container[0].uiposition);
                }
            }
            container.zIndexHack(true, options.excludeSelector);
            return loading;
        },
        /**
        * @summary 移除loading
        * @name uiLoading#remove
        * @function
        * @param options {Object} 组件的配置对象
        * @returns {jQuery} 组件对象
        */
        remove: function (options) {
            var loading= $.uiLoading.hide(options);
            loading.remove();
            loading = null;
        }
    };
    /**
    * 默认配置
    * @lends uiLoading.prototype
    */
    $.uiLoading.defaults = {
        /**
        * @type Object
        * @default body
        * @summary 组件父容器
        */
        container: $('body')
        /**
         * 在uiLoading显示/隐藏时会对指定container下的OBJECT、IFRAME对象进行隐藏/显示
         * 有时需要排除对某些组件的这种控制
         * @type String
         * @default null
         * @summary 在container中要排除的对象选择器
         */
        ,excludeSelector:null
        /**
         * 延时显示，对于加载时间较短的可以阻止遮罩显示
         * @type Number
         * @default 200
         * @summary 延时显示，对于加载时间较短的可以阻止遮罩显示
         */
        ,delay:200
        /**
         * @type String
         * @default "loading"
         * @summary 后缀标识ID
         */
        ,loadingID:'loading'
        /**
        * @type Number
        * @default 120
        * @summary 组件宽度
        */
        ,width: 120
        /**
        * @type Number
        * @default 30
        * @summary 组件高度
        */
        ,height: 30
        /**
        * @type String
        * @default ""
        * @summary 文本内容
        */
        ,text: ''
        /**
         * @type String
         * @default ""
         * @summary 显示层级
         */
        ,zIndex:888
    }
    var _add = function (options) {
        var container = options.container;
        container.setElemID();
        var contID=container.getElemID();
        var loading = $.getByParentID(contID,options.loadingID);
        if (loading.length > 0) {
        } else {
            loading = $('<div class="ui_loading"></div>')
                .appendTo(options.container).attr("id",$.getIDByParentID(contID,options.loadingID))
                .hide();
            if (container[0].tagName == 'BODY') {
                loading.css({ top: 0, left: 0 });
            }
            var mask = $.uiMask.add({
                container: loading
                ,zIndex:options.zIndex
            });
            var content = $($.uiTemplate.loading).appendTo(loading)
                .css({ width: options.width, height: options.height, zIndex: parseInt(mask.css('zIndex')) + 1 })
            if (options.text != '') {
                content.find('span.ui_loading_text').html(options.text);
            }
            $(window).bind('resize', function () {
                if (mask.is(':visible')) {
                    var content = loading.find('.ui_loading_content');
                    content.uiPosition({ horizontal: 'center', vertical: 'center', container: loading });
                }
            });
        }
        loading.hide();
        return loading;
    };
    var _show = function (options) {
        var container = options.container;
        var loading = _add(options);
        if (container.css('position') != 'relative' && container.css('position') != 'absolute') {
            container[0].uiposition = container.css('position');
            container.css('position', 'absolute');
        }
        loading.css({ width: container.width()<=10?'100%':container.width(), height: container.height()<=10?'100%':container.height() });
        var content = loading.find('.ui_loading_content');
        content.css({ width: options.width, height: options.height })
        if (options.text != '') {
            content.find('span.ui_loading_text').html(options.text);
        }
        setTimeout(function(){
            content.uiPosition({ horizontal: 'center', vertical: 'center', container: loading });
            content=null;
        },0);
        loading.children('.ui_mask').show();
        loading.show();
        container.zIndexHack(false, options.excludeSelector);
    }
})(jQuery);
